package com.yin.myproject.spring.core;

/**
 * Created by Eason on 2017/9/18 0018.
 */
public class NestedExceptionUtils {

    /**
     * Build a message for the given base message and root cause.
     * 根据给定的信息和异常获取异常信息
     * @param message
     * @param cause
     * @return
     */
    public static String buildMessage(String message,Throwable cause){
        if(cause == null){
            return message;
        }
        StringBuilder builder = new StringBuilder(64);
        if(message!=null){
            builder.append(message).append(";");
        }
        builder.append("nested exception is ").append(cause);
        return builder.toString();
    }

    /**
     * 获取最根本的异常信息
     * @param original
     * @return
     */
    public static Throwable getRootCause(Throwable original) {
        if (original == null) {
            return null;
        }
        Throwable rootCause = null;
        Throwable cause = original.getCause();
        while (cause != null && cause!=rootCause){
            rootCause =cause;
            cause=rootCause.getCause();
        }
        return cause;
    }
}
